﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
namespace QL_KHACHSAN
{
    public partial class frmMain : Office2007Form
    {
        

        private bool booDangNhap = false;
        public frmMain()
        {
            InitializeComponent();
        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                clsMain.flexConn.DatabaseConnection(false);
                Form frm = new frmSoTang();
                KhoiTaoForms(frm);
                 frm = new frmPhong();
                KhoiTaoForms(frm);
                frm = new Forms.frmDatPhong();
                KhoiTaoForms(frm);
                frm = new Forms.frmLoaiPhong();
                KhoiTaoForms(frm);
                if (clsMain.flexConn.fcConn != null)
                {
                    timerMain.Enabled = true;

                }
                tabForms.Parent = this;
            }

            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
        private void DangNhap()
        {
            try
            {
                if (booDangNhap == false)
                {
                    DangNhapToolStripMenuItem.Text = "Đăng xuất";
                }
                else
                {
                    booDangNhap = false;
                    clsMain.booLogin = false;
                    DangNhapToolStripMenuItem.Text = "Đăng nhập";
                    trvMenu.Nodes.Clear();
                }

                Form frm = new Forms.frmDangNhap();


                frm.ShowDialog();

                if (clsMain.booLogin == true)
                {
                    ConnectSystem();
                    booDangNhap = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
        private void ConnectSystem()
        {
            try
            {
                string strSQLParent = "";
                string strSQLChild = "";
                strSQLParent = "select Code,Data,IconID from System_Menus";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs where Parent_Code = ";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs, QuyenNguoiDung where  QuyenNguoiDung.USER_CODE='" + clsMain.strUserLogin + "' and System_Childs.Code=QuyenNguoiDung.SYSCHILD_CODE and System_Childs.Parent_Code = ";
                clsMain cls = new clsMain();
                cls.AddDataToTreeView(strSQLParent, strSQLChild, trvMenu);

            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
        private void KhoiTaoForms(System.Windows.Forms.Form frm)
        {
            try
            {

                // frm.MdiParent = this;
                //Khởi tạo form
                clsMain.flexConn.InitForms(ref frm, clsMain.flexConn.fcConn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }

        private void trvMenu_DoubleClick(object sender, EventArgs e)
        {
            try
            {

                CallForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
        private void CallForm()
        {
            try
            {
                if (trvMenu.SelectedNode.Tag != null)
                {
                    string strTag = Convert.ToString(trvMenu.SelectedNode.Tag);

                    int intIDForm = Convert.ToInt32(strTag);

                    if (clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booActive == true)
                    {
                        int i = 0;
                        bool isShowFlag = false;

                        for (; i < tabForms.TabPages.Count; i++)
                        {
                            if (tabForms.TabPages[i].Tag == clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag)
                            {
                                if (tabForms.TabPages[i].PageVisible == false)
                                    tabForms.TabPages[i].PageVisible = true;
                                tabForms.SelectedTabPage = tabForms.TabPages[i];

                                isShowFlag = true;
                            }
                        }
                        if (isShowFlag == false)
                        {
                            if (intIDForm !=13)//Code tạm
                            {
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booIsShow = true;
                                clsMain.flexConn.LoadListView(intIDForm);
                                clsMain.flexConn.GenerateCode(intIDForm);
                                clsMain.flexConn.ClearTextControll(intIDForm);
                               
                                //clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.TopLevel = false;//Sẽ bị lỗi không thể select text trong textbox
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.BringToFront();//phải code trước mMdiParent = this;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.MdiParent = this;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Visible = true;
                                tabForms.TabPages.Add();
                                tabForms.TabPages[tabForms.TabPages.Count - 1].Controls.Add(clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm);
                                tabForms.TabPages[tabForms.TabPages.Count - 1].BackColor = System.Drawing.Color.AliceBlue;
                                tabForms.TabPages[tabForms.TabPages.Count - 1].Tag = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag;
                                tabForms.SelectedTabPage = tabForms.TabPages[tabForms.TabPages.Count - 1];
                                tabForms.TabPages[tabForms.TabPages.Count - 1].Text = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Text;
                                tabForms.TabPages[tabForms.TabPages.Count - 1].ImageIndex = trvMenu.SelectedNode.ImageIndex;
                                
                                
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Show();


                            }
                            else if (intIDForm == 13)//Code tạm
                            {
                                frmSoTang frm = new frmSoTang();
                                frm.Show();
                            }
                        }
                    }


                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }

        private void tabForms_CloseButtonClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraTab.XtraTabControl tabControl = sender as DevExpress.XtraTab.XtraTabControl;
                DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs arg = e as DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs;
                //  (arg.Page as DevExpress.XtraTab.XtraTabPage ).PageVisible = false;
                tabForms.TabPages.Remove((arg.Page as DevExpress.XtraTab.XtraTabPage));
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
        private int intTick=0;
        private void timerMain_Tick(object sender, EventArgs e)
        {
            try
            {
                if (intTick >= 3)
                {
                    timerMain.Enabled = false;
                    DangNhap();
                    
                }
                else {
                    intTick++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(clsMain.STR_ERROR + ex.ToString());
            }
        }
    }
}
